﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using FrameWork.Entity;
using FrameWork.web.Code;
using FrameWork.Components;

namespace FrameWork.web.Module.ITMS.Service
{
    public partial class Service : System.Web.UI.Page
    {
        /// <summary>
        /// 二次派单人员通知方式(0:不填 1:邮件 2:短消息 3: 全选)
        /// </summary>
        protected int SecUserNotify
        {

            get
            {
                if (chkSecUserEmail.Checked && chkSecUserMsg.Checked)
                {
                    return 3;
                }
                else if (chkSecUserEmail.Checked)
                {
                    return 1;
                }
                else if (chkSecUserMsg.Checked)
                {
                    return 2;
                }
                else
                {
                    return 0;
                }

            }
        }

        /// <summary>
        ///  二次派单关注人员通知方式(0:不填 1:邮件 2:短消息 3: 全选)
        /// </summary>
        protected int SecAttNotify
        {

            get
            {
                if (chkSecAttEmail.Checked && chkSecAttMsg.Checked)
                {
                    return 3;
                }
                else if (chkSecAttEmail.Checked)
                {
                    return 1;
                }
                else if (chkSecAttMsg.Checked)
                {
                    return 2;
                }
                else
                {
                    return 0;
                }

            }
        }

        /// <summary>
        /// 硬件更换信息通知方式(0:不填 1:邮件 2:短消息 3: 全选)
        /// </summary>
        protected int HardNoticeType
        {

            get
            {
                if (chkHardNoticeEmail.Checked && chkHardNoticeMsg.Checked)
                {
                    return 3;
                }
                else if (chkHardNoticeEmail.Checked)
                {
                    return 1;
                }
                else if (chkHardNoticeMsg.Checked)
                {
                    return 2;
                }
                else
                {
                    return 0;
                }

            }
        }

        /// <summary>
        /// IP地址更换信息通知方式(0:不填 1:邮件 2:短消息 3: 全选)
        /// </summary>
        protected int IpAddrNoticeType
        {

            get
            {
                if (chkIPAddrNotifyEmail.Checked && chkIPAddrNotifyMsg.Checked)
                {
                    return 3;
                }
                else if (chkIPAddrNotifyEmail.Checked)
                {
                    return 1;
                }
                else if (chkIPAddrNotifyMsg.Checked)
                {
                    return 2;
                }
                else
                {
                    return 0;
                }

            }
        }

        /// <summary>
        /// 绑定解决方案
        /// </summary>
        private void bindResolvent()
        {
            its_ResolventEntity re = new its_ResolventEntity();
            re.UID = 0;
            its_ResolventCs reCs = new its_ResolventCs();
            DataTable dt = reCs.GetResolvent(re);
            DataRow dr = dt.NewRow();
            dr["UID"] = -1;
            dr["Title"] = "请选择";
            dt.Rows.InsertAt(dr, 0);
           
            this.drpResolv.DataValueField = "UID";
            this.drpResolv.DataTextField = "Title";
            this.drpResolv.DataSource = dt;
            this.drpResolv.DataBind();
            this.drpResolv.SelectedIndex = -1;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                GetMailInfo();
                BindUnsolve();
                BindSolve();
                BindAttachSolve();
                BindAttachUnSolve();
                BindAttend();

                //绑定故障分类
                sys_FieldValue sfTroubType = new sys_FieldValue();
                sfTroubType.V_F_Key = "TroubType";
                its_FieldValueCs sfTroubTypeCs = new its_FieldValueCs();
                DataTable dtTroubType = sfTroubTypeCs.Field_SelectInfoByF_Key(sfTroubType);
                this.drpServTrobType.DataTextField = "V_Text";
                this.drpServTrobType.DataValueField = "ValueID";
                this.drpServTrobType.DataSource = dtTroubType;
                this.drpServTrobType.DataBind();
                //绑定问题分类
                sys_FieldValue sfProbType = new sys_FieldValue();
                sfProbType.V_F_Key = "ProbType";
                its_FieldValueCs sfProbTypeCs = new its_FieldValueCs();
                DataTable dtsfProbType = sfProbTypeCs.Field_SelectInfoByF_Key(sfProbType);
                this.drpServProbType.DataTextField = "V_Text";
                this.drpServProbType.DataValueField = "ValueID";
                this.drpServProbType.DataSource = dtsfProbType;
                this.drpServProbType.DataBind();

                bindResolvent();
            }
        }

        /// <summary>
        /// 绑定未处理问题单
        /// </summary>
        private void BindUnsolve()
        {
                its_ServiceDocCs its = new its_ServiceDocCs();
                ArrayList arr = new ArrayList();
                sys_User us=new sys_User();
                us.UserID = Common.Get_UserID;
                arr = its.its_unsolveServiceDoc(this.PagerUnsolve.PageSize, this.PagerUnsolve.CurrentPageIndex, us);
                this.GVUnsolve.DataSource = (DataTable)arr[1];
                this.GVUnsolve.DataBind();
                this.PagerUnsolve.RecordCount = (int)arr[0];
        }


        /// <summary>
        /// 绑定已经解决问题单
        /// </summary>
        private void BindSolve()
        {
            its_ServiceDocCs its = new its_ServiceDocCs();
            ArrayList arr = new ArrayList();
            sys_User us = new sys_User();
            us.UserID = Common.Get_UserID;
            arr = its.its_solveServiceDoc(this.PagerSolve.PageSize, this.PagerSolve.CurrentPageIndex, us);
            this.GVSolve.DataSource = (DataTable)arr[1];
            this.GVSolve.DataBind();
            this.PagerSolve.RecordCount = (int)arr[0];
        }

        /// <summary>
        /// 绑定参与解决并关闭问题单
        /// </summary>
        private void BindAttachSolve()
        {
            its_ServiceDocCs its = new its_ServiceDocCs();
            ArrayList arr = new ArrayList();
            sys_User us = new sys_User();
            us.UserID = Common.Get_UserID;
            arr = its.its_AttachSolveServiceDoc(this.PagerAttachSolve.PageSize, this.PagerAttachSolve.CurrentPageIndex, us);
            this.GVAttachSolve.DataSource = (DataTable)arr[1];
            this.GVAttachSolve.DataBind();
            this.PagerAttachSolve.RecordCount = (int)arr[0];
        }

        /// <summary>
        /// 绑定参与解决未关闭问题单
        /// </summary>
        private void BindAttachUnSolve()
        {
            its_ServiceDocCs its = new its_ServiceDocCs();
            ArrayList arr = new ArrayList();
            sys_User us = new sys_User();
            us.UserID = Common.Get_UserID;
            arr = its.its_AttachUnSolveServiceDoc(this.PagerAttachUnSolve.PageSize, this.PagerAttachUnSolve.CurrentPageIndex, us);
            this.GVAttachUnSolve.DataSource = (DataTable)arr[1];
            this.GVAttachUnSolve.DataBind();
            this.PagerAttachUnSolve.RecordCount = (int)arr[0];
        }

        /// <summary>
        /// 绑定我关注的问题单
        /// </summary>
        private void BindAttend()
        {
            its_ServiceDocCs its = new its_ServiceDocCs();
            ArrayList arr = new ArrayList();
            sys_User us = new sys_User();
            us.UserID = Common.Get_UserID;
            arr = its.its_AttendServiceDoc(this.PagerAttend.PageSize, this.PagerAttend.CurrentPageIndex, us);
            this.GVAttend.DataSource = (DataTable)arr[1];
            this.GVAttend.DataBind();
            this.PagerAttend.RecordCount = (int)arr[0];
        }


        protected void PagerUnsolve_PageChanged(object sender, EventArgs e)
        {
            BindUnsolve();
            showTR();
        }

        protected void PagerSolve_PageChanged(object sender, EventArgs e)
        {
            BindSolve();
            hindTR();
        }

        protected void PagerAttachSolve_PageChanged(object sender, EventArgs e)
        {
            BindAttachSolve();
            hindTR();
        }

        protected void PagerAttachUnSolve_PageChanged(object sender, EventArgs e)
        {
            BindAttachUnSolve();
            hindTR();
        }

        protected void PagerAttend_PageChanged(object sender, EventArgs e)
        {
            BindAttend();
            hindTR();
        }

        /// <summary>
        /// 隐藏不需要的TR
        /// </summary>
        private void hindTR()
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "hindTR", "<script>hindTR();</script>");
        }

        /// <summary>
        /// 显示TR
        /// </summary>
        private void showTR()
        {
            this.ClientScript.RegisterStartupScript(this.GetType(), "showTR", "<script>showTR();</script>");
        }


        protected void lbtUnsolve_Click(object sender, EventArgs e)
        {
            try
            {
                
                if (((LinkButton)sender).Parent.Parent.Parent.Parent.ID == "GVUnsolve")
                {
                    txtUid.Text = ((Label)((GridViewRow)((LinkButton)sender).Parent.Parent).FindControl("labUID")).Text.Trim();
                    showTR();
                    GetDocDetail(txtUid.Text.Trim());
                }
                else
                {
                    hindTR();
                }
                string strServDocNo = ((LinkButton)sender).Text;
                its_ServiceDocCs sdCs = new its_ServiceDocCs();
                its_ServiceDoc sd = new its_ServiceDoc();
                sd.ServDocNo = strServDocNo;
                DataTable dt = sdCs.ServiceSelect(sd);
                txtServDocNo.Text = strServDocNo;
                txtRepContext.Text = dt.Rows[0]["RepContent"].ToString();
                txtRepTitle.Text = dt.Rows[0]["RepTitle"].ToString();
                txtSolution.Text = dt.Rows[0]["ItemMessage"].ToString().Replace("\\n", "\n");
                txtSolution.Text = txtSolution.Text.Replace("\\t", "\t");
                
                drpServTrobType.SelectedValue = dt.Rows[0]["ServTrobType"].ToString();
                drpServProbType.SelectedValue = dt.Rows[0]["ServProbType"].ToString();
            }
            catch
            {
                //
            }
        }

        private void GetDocDetail(string DocID)
        {
            its_ServiceDocCs serDoc = new its_ServiceDocCs();
            DataTable dt = serDoc.GetDocDetail(DocID);

            txtMySolution.Text = dt.Rows[0]["Solution"].ToString().Trim();
            txtHardwareInfo.Text = dt.Rows[0]["HardwareInfo"].ToString().Trim();
            txtIPAddressInfo.Text = dt.Rows[0]["IPAddressInfo"].ToString().Trim();
            txtContent.Text = dt.Rows[0]["Content"].ToString().Trim();
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            if (Check())
            {
                //存在二次派单则只能点击问题关闭
                if (chkSecAlloc.Checked)
                {
                    this.ClientScript.RegisterStartupScript(this.GetType(), "key7", "<script>alert('存在二次派单,只能点击『完成』!');</script>");
                    return;
                }
                DocFinish();
            }
        }

        private bool Check()
        {
            if (txtMySolution.Text.Trim() == "")
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "key1", "<script>alert('解决方法内容不能为空');</script>");
                return false;
            }
            return true;
        }

        private void GetMailInfo()
        {
            its_FieldValueCs fvCs = new its_FieldValueCs();
            sys_FieldValue fv = new sys_FieldValue();

            fv.V_F_Key = "MailServer";
            DataTable dtMailServer = fvCs.Field_SelectInfoByF_Key(fv);

            fv.V_F_Key = "MailUser";
            DataTable dtMailUser = fvCs.Field_SelectInfoByF_Key(fv);

            fv.V_F_Key = "MailPwd";
            DataTable dtMailPwd = fvCs.Field_SelectInfoByF_Key(fv);


            fv.V_F_Key = "MailAddr";
            DataTable dtMailAddr = fvCs.Field_SelectInfoByF_Key(fv);


            lblMailServer.Text = dtMailServer.Rows[0]["V_Text"].ToString();
            lblMailUser.Text = dtMailUser.Rows[0]["V_Text"].ToString();
            lblMailPwd.Text = dtMailPwd.Rows[0]["V_Text"].ToString();
            lblMailAddr.Text = dtMailAddr.Rows[0]["V_Text"].ToString();
        }

        private void DocClose()
        {
            try
            {
                //服务单类
                its_ServiceDoc serDoc = new its_ServiceDoc(); 
                //新的二次派单类
                its_ServiceAllotDoc serSecAllocDoc = new its_ServiceAllotDoc();
                //二次派单处理类
                its_ServiceAllotDoc serSecAllocDocOld = new its_ServiceAllotDoc();

                serDoc.ServDocNo = txtServDocNo.Text.Trim();
                serDoc.ServProbType = Convert.ToInt32(drpServProbType.SelectedValue);
                serDoc.ServTrobType = Convert.ToInt32(drpServTrobType.SelectedValue);
                serDoc.Statue = 0;

                //处理派单信息
                serSecAllocDocOld.UID = Int32.Parse(txtUid.Text.Trim());
                serSecAllocDocOld.DOCNO = txtServDocNo.Text.Trim();
                serSecAllocDocOld.Solution = txtMySolution.Text.Trim();
                serSecAllocDocOld.HardwareInfo = txtHardwareInfo.Text.Trim();
                serSecAllocDocOld.HardNoticeType = HardNoticeType;
                serSecAllocDocOld.IPAddressInfo = txtIPAddressInfo.Text.Trim();
                serSecAllocDocOld.IPNotifyType = IpAddrNoticeType;
                serSecAllocDocOld.Content = txtContent.Text.Trim();

                if (chkSecAlloc.Checked)
                {
                    serSecAllocDocOld.DealWith = true;
                }
                else
                {
                    serSecAllocDocOld.DealWith = false;
                }


                if (chkSecAlloc.Checked)
                {
                    if (txtSecUserID.Text.Trim() == "")
                    {
                        this.ClientScript.RegisterStartupScript(this.GetType(), "key6", "<script>alert('请选择二次派单人员!');</script>");
                        return;
                    }

                    serSecAllocDoc.DOCNO = txtServDocNo.Text.Trim();
                    serSecAllocDoc.AllotPSNID = Convert.ToInt32(txtSecUserID.Text.Trim());
                    serSecAllocDoc.AllotNoticeType = SecUserNotify;
                    if (txtSecAttID.Text.Trim() != "")
                    {
                        serSecAllocDoc.AttentPSNID = Convert.ToInt32(txtSecAttID.Text.Trim());
                        serSecAllocDoc.AttentNoticeType = SecAttNotify;
                    }
                    serSecAllocDoc.Status = 1;
                    serSecAllocDoc.DealWith = false;
                    serSecAllocDoc.DelegUID = Common.Get_UserID;

                }

                
                its_ServiceDocCs sdCs = new its_ServiceDocCs();
                if (chkSecAlloc.Checked)
                {
                    sdCs.ServiceAllotDoc_Add(serDoc, serSecAllocDoc, serSecAllocDocOld);
                }
                else
                {
                    sdCs.Service_Finish(serDoc, serSecAllocDocOld);
                }


                //硬件更换通知
                switch (serSecAllocDocOld.HardNoticeType)
                {
                    case 1:
                        sdCs.SendMailToHardwareAdmin(lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "硬件更换信息通知", serSecAllocDocOld.HardwareInfo);
                        break;
                    case 2:
                        sdCs.SendMsgToHardWareAdmin(serSecAllocDocOld.HardwareInfo);
                        break;
                    case 3:
                        sdCs.SendMsgToHardWareAdmin(serSecAllocDocOld.HardwareInfo);
                        sdCs.SendMailToHardwareAdmin(lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "硬件更换信息通知", serSecAllocDocOld.HardwareInfo);
                        break;
                    default:
                        break;
                }

                //IP地址更换通知
                switch (serSecAllocDocOld.IPNotifyType)
                {
                    case 1:
                        sdCs.SendMailToPCAdmin(lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "IP地址更换通知", serSecAllocDocOld.IPAddressInfo);
                        break;
                    case 2:
                        sdCs.SendMsgToPCAdmin(serSecAllocDocOld.IPAddressInfo);
                        break;
                    case 3:
                        sdCs.SendMsgToPCAdmin(serSecAllocDocOld.IPAddressInfo);
                        sdCs.SendMailToPCAdmin(lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "IP地址更换通知", serSecAllocDocOld.IPAddressInfo);
                        break;
                    default:
                        break;
                }

                //二次派单通知
                string tempStr = "";
                if (chkSecAlloc.Checked)
                {

                    tempStr = "您有一条待处理的二次派单：" + txtServDocNo.Text.Trim() + "，请及时处理。";
                    switch (serSecAllocDoc.AllotNoticeType)
                    {
                        case 1:
                            sdCs.SendMailTo(serSecAllocDoc.AllotPSNID, lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "派单信息通知", tempStr);
                            break;
                        case 2:
                            sdCs.SendMsgTo(serSecAllocDoc.AllotPSNID, tempStr);
                            break;
                        case 3:
                            sdCs.SendMsgTo(serSecAllocDoc.AllotPSNID, tempStr);
                            sdCs.SendMailTo(serSecAllocDoc.AllotPSNID, lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "派单信息通知", tempStr);
                            break;
                        default:
                            break;
                    }

                    if (txtSecAttID.Text.Trim() != "")
                    {
                        tempStr = "您有一条关注的二次派单：" + txtServDocNo.Text.Trim() + "，请及时处理。";

                        switch (serSecAllocDoc.AttentNoticeType)
                        {
                            case 1:
                                sdCs.SendMailTo(serSecAllocDoc.AttentPSNID, lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "派单信息通知", tempStr);
                                break;
                            case 2:
                                sdCs.SendMsgTo(serSecAllocDoc.AttentPSNID, tempStr);
                                break;
                            case 3:
                                sdCs.SendMsgTo(serSecAllocDoc.AttentPSNID, tempStr);
                                sdCs.SendMailTo(serSecAllocDoc.AttentPSNID, lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "派单信息通知", tempStr);
                                break;
                            default:
                                break;
                        }
                    }
                }

            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "key6", "<script>alert('服务单二次派单保存出错!" + ex.Message.Replace("\n", "\\n") + "');</script>");
                //this.ClientScript.RegisterStartupScript(this.GetType(), "key6", "<script>alert('服务单二次派单保存出错!');</script>");
                FileTxtLogs.WriteLog("服务单二次派单保存出错,错误原因:" + ex.Message);
                return;
            }

            EventMessage.MessageBox(1, "保存成功", "服务单保存成功!", Icon_Type.OK, this.Request.Url.ToString());
        }

        private void DocFinish()
        {
            try
            {
                //服务单主表类
                its_ServiceDoc serDoc = new its_ServiceDoc();
                //服务单派单表
                its_ServiceAllotDoc serAllocDoc = new its_ServiceAllotDoc();

                serDoc.ServDocNo = txtServDocNo.Text.Trim();
                serDoc.Statue = 1;
                serDoc.ServProbType = Convert.ToInt32(drpServProbType.SelectedValue);
                serDoc.ServTrobType = Convert.ToInt32(drpServTrobType.SelectedValue);
                serDoc.DocSolvePSN = Common.Get_UserID;


                serAllocDoc.UID = Int32.Parse(txtUid.Text.Trim());
                serAllocDoc.HardwareInfo = txtHardwareInfo.Text.Trim();
                serAllocDoc.HardNoticeType = HardNoticeType;
                serAllocDoc.IPAddressInfo = txtIPAddressInfo.Text.Trim();
                serAllocDoc.IPNotifyType = IpAddrNoticeType;
                serAllocDoc.Solution = txtMySolution.Text.Trim();
                serAllocDoc.Content = txtContent.Text.Trim();
                serAllocDoc.DealWith = true;

                its_ServiceDocCs sdCs = new its_ServiceDocCs();
                sdCs.Service_Finish(serDoc, serAllocDoc);

                //硬件更换通知
                switch (serAllocDoc.HardNoticeType)
                {
                    case 1:
                        sdCs.SendMailToHardwareAdmin(lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "硬件更换信息通知", serAllocDoc.HardwareInfo);
                        break;
                    case 2:
                        sdCs.SendMsgToHardWareAdmin(serAllocDoc.HardwareInfo);
                        break;
                    case 3:
                        sdCs.SendMsgToHardWareAdmin(serAllocDoc.HardwareInfo);
                        sdCs.SendMailToHardwareAdmin(lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "硬件更换信息通知", serAllocDoc.HardwareInfo);
                        break;
                    default:
                        break;
                }

                //IP地址更换通知
                switch (serAllocDoc.IPNotifyType)
                {
                    case 1:
                        sdCs.SendMailToPCAdmin(lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "IP地址更换通知", serAllocDoc.IPAddressInfo);
                        break;
                    case 2:
                        sdCs.SendMsgToPCAdmin(serAllocDoc.IPAddressInfo);
                        break;
                    case 3:
                        sdCs.SendMsgToPCAdmin(serAllocDoc.IPAddressInfo);
                        sdCs.SendMailToPCAdmin(lblMailAddr.Text.Trim(), lblMailServer.Text.Trim(), lblMailUser.Text.Trim(), lblMailPwd.Text.Trim(), "IP地址更换通知", serAllocDoc.IPAddressInfo);
                        break;
                    default:
                        break;
                }
            }
            catch (Exception ex)
            {
                this.ClientScript.RegisterStartupScript(this.GetType(), "key6", "<script>alert('服务单保存出错!" + ex.Message.Replace("\n", "\\n") + "');</script>");
                //this.ClientScript.RegisterStartupScript(this.GetType(), "key6", "<script>alert('服务单保存出错!');</script>");
                FileTxtLogs.WriteLog("服务单保存出错,错误原因:" + ex.Message);
                return;
            }
            EventMessage.MessageBox(1, "保存成功", "服务单保存成功!", Icon_Type.OK, this.Request.Url.ToString());
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            //进行二次派单转派
            if (Check())
            {
                DocClose();
            }
        }        

    }
}
